#include "IMGroup.h"
#include "IMUser.h"
#include "IMUserManager.h"

void IMGroup::addUser(const userid_t &uid)
{
    auto u = IMUserManager::getInstance()->FindUser(uid);
    addUser(u);
}

void IMGroup::addUser(const IMUserPtr &user)
{
    for (auto &u : users_)
    {
        if (u->getUid() == user->getUid())
        {
            return;
        }
    }
    if (user != nullptr)
    {
        users_.push_back(user);
    }
}

void IMGroup::removeUser(const userid_t &uid)
{
    auto u = IMUserManager::getInstance()->FindUser(uid);
    removeUser(u);
}

void IMGroup::removeUser(const IMUserPtr &user)
{
    for (auto it = users_.begin(); it != users_.end(); ++it)
    {
        if ((*it)->getUid() == user->getUid())
        {
            users_.erase(it);
            user->removeGroup(gid_);
            return;
        }
    }
}

void IMGroup::removeAll()
{
    for (auto &u : users_)
    {
        // u->removeGroup(gid_);
        u->removeGroup(gid_);
    }
}
